<template>
  <div class="app-container">
    <div class="box">
      <div class="box ordersetting">
        <div class="calendar">
          <!-- 年份 月份 -->
          <div class="month">
            <div class="currentdate">
              <span class="choose-year">{{ currentYear }}年</span>
              <span class="choose-month">{{ currentMonth }}月</span>
            </div>
            <div class="choose">
              <el-date-picker
                class="data-picker"
                type="month"
                value-format="yyyy-MM-dd"
                v-model="dateValue"
                placeholder="选择月"
                @change="changeDate"
              >
              </el-date-picker>
              <span
                @click="goCurrentMonth(currentYear, currentMonth)"
                class="gotoday"
              >今天</span
              >
              <span @click="pickPre(currentYear, currentMonth)">❮</span>
              <span @click="pickNext(currentYear, currentMonth)">❯</span>
            </div>
            <div class="clearfix"></div>
          </div>
          <!-- 星期 -->
          <div class="caldate">
            <ul class="weekdays">
              <li>周一</li>
              <li>周二</li>
              <li>周三</li>
              <li>周四</li>
              <li>周五</li>
              <li>周六</li>
              <li>周日</li>
            </ul>
            <!-- 日期 -->
            <ul class="days">
              <!-- v-for循环 每一次循环用<li>标签创建一天 -->
              <li v-for="(dayobject, index) in days" :key="index">
                <template>
                  <!-- 非当前月份 -->
                  <div
                    class="other-month"
                    v-if="dayobject.day.getMonth() + 1 !== currentMonth"
                  >
                    {{ dayobject.day.getDate() }}
                  </div>
                  <!-- 当前月 -->
                  <div
                    class="everyday"
                    v-if="dayobject.day.getMonth() + 1 === currentMonth"
                  >
                    <span class="datenumber">{{
                        dayobject.day.getDate()
                      }}</span>

                    <template>
                      <template v-for="obj in leftobj">
                        <template v-if="obj.day === dayobject.day.getDate()">
                          <template>
                            <div class="usual">
                              <p>已预约</p>
                            </div>
                          </template>
                        </template>
                      </template>
                      <button
                        v-if="dayobject.day > today && !daysCurrentMonth.includes(dayobject.day.getDate())"
                        @click="handleOrderSet(dayobject.day)"
                        class="orderbtn"
                      >
                        设置
                      </button>
                      <button
                        v-else-if="daysCurrentMonth.includes(dayobject.day.getDate())"
                        class="orderbtn"
                        @click="handleOrderDel(dayobject.day)"
                      >
                        取消设置
                      </button>
                    </template>
                  </div>
                </template>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { addDate, listDate, delDate } from '@/api/system/subscribe/subscribedate'
export default {
  name: "OrderSetting",
  data() {
    return {
      dateValue: new Date(),
      today: new Date(), //当前日期
      currentDay: 1,
      currentMonth: 1,
      LocalMonth: 1,
      currentYear: 1970,
      currentWeek: 1,
      days: [],
      leftobj: [], //用于装载页面显示的月份已经进行预约设置的数据
      daysCurrentMonth: [],
    };
  },
  created() {
    //在vue初始化时调用
    this.initData(null);
  },
  methods: {
    // 修改时间
    changeDate(val) {
      this.initData(val);
    },
    handleOrderDel(day) {
      this.$alert("是否取消？", "预约设置", {
        confirmButtonText: "确定"
      })
        .then(() => {
          delDate(day.getFullYear(), day.getMonth() + 1, day.getDate()).then((response) => {
            if (response.code === 200) {
              this.initData(
                this.formatDate(day.getFullYear(), day.getMonth() + 1, 1)
              );
              this.$message({
                type: "success",
                message: '取消成功',
              });
            } else {
              this.$message.error('取消失败');
            }
          })
        })
    },
    //预约设置
    handleOrderSet(day) {
      this.$alert("是否确定设置今天为可预约？", "预约设置", {
        confirmButtonText: "确定"
      })
        .then(() => {
          addDate({
            year: day.getFullYear(),
            month: day.getMonth() + 1,
            day: day.getDate()
          }).then((response) => {
            if (response.code === 200) {
                this.initData(
                  this.formatDate(day.getFullYear(), day.getMonth() + 1, 1)
                );
                this.$message({
                  type: "success",
                  message: '设置成功',
                });
              } else {
                this.$message.error('设置失败');
              }
          })
        })
    },
    //上传之前进行文件格式校验
    beforeUpload(file) {
      const isXLS = file.type === "application/vnd.ms-excel";
      if (isXLS) {
        return true;
      }
      const isXLSX =
        file.type ===
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      if (isXLSX) {
        return true;
      }
      this.$message.error("上传文件只能是xls或者xlsx格式!");
      return false;
    },
    //下载模板文件
    downloadTemplate() {
      window.location.href = "/template/ordersetting_template.xlsx";
    },
    //上传成功提示
    handleSuccess(response, file) {
      if (response.flag) {
        this.$message({
          message: response.message,
          type: "success",
        });
      } else {
        this.$message.error(response.message);
      }
    },
    //初始化当前页要展示的日期
    initData(cur) {
      var date;
      var index = 0; //控制显示预定的天数
      if (cur) {
        date = new Date(cur);
      } else {
        var now = new Date();
        var d = new Date(this.formatDate(now.getFullYear(), now.getMonth(), 1));
        d.setDate(35);
        date = new Date(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
      }
      this.currentDay = date.getDate();
      this.currentYear = date.getFullYear();
      this.currentMonth = date.getMonth() + 1;
      this.currentWeek = date.getDay(); // //本月第一天是周几（周日0 周六 6）
      var today = new Date();
      this.LocalMonth = today.getMonth() + 1;
      if (this.currentWeek === 0) {
        this.currentWeek = 7;
      }
      var str = this.formatDate(
        this.currentYear,
        this.currentMonth,
        this.currentDay
      );
      this.days.length = 0;
      // 今天是周日，放在第一行第7个位置，前面6个
      //初始化本周
      for (var i = this.currentWeek - 1; i >= 0; i--) {
        var d = new Date(str);
        d.setDate(d.getDate() - i);
        var dayobject = {};
        dayobject.day = d;
        var now = new Date();
        if (
          d.getDate() === now.getDate() &&
          d.getMonth() === now.getMonth() &&
          d.getFullYear() === now.getFullYear()
        ) {
          dayobject.index = index++; //从今天开始显示供预定的数量
        } else if (index !== 0 && index < 3) dayobject.index = index++; //从今天开始3天内显示供预定的数量
        this.days.push(dayobject); //将日期放入data 中的days数组 供页面渲染使用
      }
      //其他周
      for (var i = 1; i <= 35 - this.currentWeek; i++) {
        var d = new Date(str);
        d.setDate(d.getDate() + i);
        var dayobject = {}; //dayobject   {day:date,index:2}
        dayobject.day = d;
        var now = new Date();
        if (
          d.getDate() === now.getDate() &&
          d.getMonth() === now.getMonth() &&
          d.getFullYear() === now.getFullYear()
        ) {
          dayobject.index = index++;
        } else if (index !== 0 && index < 3) dayobject.index = index++;
        this.days.push(dayobject);
      }
      //发送ajax请求，根据当前页面对应的月份查询预约设置信息
      var that = this;
      listDate({year: this.currentYear, month: this.currentMonth}).then(
        (response) => {
          if (response.code === 200) {
            //为模型数据赋值，绑定到日历中
            this.leftobj = response.rows;
            this.daysCurrentMonth = []
            this.leftobj.forEach(item => {
              this.daysCurrentMonth.push(item.day)
            })
          } else {
            this.$message.error("查询失败");
          }
        }
      );
      // this.leftobj = [
      //                   { date: 1, number: 120, reservations: 1 },
      //                   { date: 3, number: 120, reservations: 1 },
      //                   { date: 4, number: 120, reservations: 120 },
      //                   { date: 6, number: 120, reservations: 1 },
      //                   { date: 10, number: 120, reservations: 1 }
      //               ];
    },
    //切换到当前月份
    goCurrentMonth(year, month) {
      var d = new Date();
      this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
    },
    //向前一个月
    pickPre(year, month) {
      // setDate(0); 上月最后一天
      // setDate(-1); 上月倒数第二天
      // setDate(dx) 参数dx为 上月最后一天的前后dx天
      var d = new Date(this.formatDate(year, month, 1));
      d.setDate(0);
      this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
    },
    //向后一个月
    pickNext(year, month) {
      var d = new Date(this.formatDate(year, month, 1));
      d.setDate(35); ////获取指定天之后的日期
      this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
    },
    // 返回 类似 2016-01-02 格式的字符串
    formatDate(year, month, day) {
      var y = year;
      var m = month;
      if (m < 10) m = "0" + m;
      var d = day;
      if (d < 10) d = "0" + d;
      return y + "-" + m + "-" + d;
    },
  },
};
</script>

<style lang="scss" scoped>
.ordersetting .el-input__inner {
  height: 36px;
  line-height: 36px;
}
.ordersetting {
  background: #f5f5f5;
  box-shadow: none;
  border-top: none;
}
.gotoday {
  border: 1px solid #d5d9df
}
.calendar p {
  margin: 0;
}
.filebtn {
  padding: 9px 20px;
  border: none;
  font-size: 18px;
  background: rgb(170, 34, 144);
}

.data-picker {
  margin-right: 10px;
}

.month {
  width: 100%;
  line-height: 3;
  /* background: #00B8EC; */
}

.month ul {
  margin: 0;
  padding: 0;
  display: flex;
}

.year-month {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
}

.year-month:hover {
  background: rgba(150, 2, 12, 0.1);
}

.choose-year {
  padding-left: 20px;
  font-size: 1.2rem;
}

.choose-month {
  text-align: center;
  font-size: 1.2rem;
}

.arrow {
  padding: 30px;
}

.arrow:hover {
  background: rgba(100, 2, 12, 0.1);
}

.month ul li {
  color: white;
  font-size: 20px;
  text-transform: uppercase;
  letter-spacing: 3px;
}
.caldate {
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.1), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
}
.weekdays {
  margin: 0;
  padding: 0;
  background-color: #fff;
  display: flex;
  flex-wrap: wrap;
  color: #333;
  justify-content: space-around;
  border-bottom: 1px solid #ededed;
  border-top: 2px solid rgb(170, 34, 144);
}

.weekdays li {
  display: inline-block;
  width: 14%;
  text-align: center;
  padding: 10px 0;
  border-right: 1px solid #f1efef;
  color: #000;
  font-size: 20px;

}

.days {
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;

}

.days li {
  height: 120px;
  list-style-type: none;
  display: inline-block;
  width: 14%;
  text-align: right;
  font-size: 1rem;
  color: #000;
  list-style-type: none;
  display: inline-block;
  width: 14.2%;
  font-size: 1rem;
  color: #000;
  border-bottom: 1px solid #fff;
}
.everyday {
  height: 120px;
  border-bottom: 1px solid #f1efefef;
  position: relative;
  background: #fff;
}
.everyday .datenumber {
  position: absolute;
  right: 10px;
  top: 10px;
}
.everyday .usual {
  height: 100%;
  background: rgb(170, 34, 144);
  text-align: center;
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  font-size: 20px;
  line-height: 1.5;
}
.everyday .fulled {
  height: 100%;
  background: #ff9c9d;
  text-align: center;
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  font-size: 20px;
}
.everyday .nochoose {
  height: 100%;
  background: #effbff;
}
.everyday .orderbtn {
  position: absolute;
  right: 10px;
  bottom: 10px;
  cursor: pointer;
  border: none;
  background: #aa2290;
  padding: 3px 10px;
  color: #fff;
  border-radius: 4px;
  font-size: 16px;
}
.days li .active {
  padding: 6px 10px;
  border-radius: 50%;
  background: rgb(170, 34, 144);
  color: #fff;
}

.days li .other-month {
  padding: 10px;
  height: 120px;
  background: #f9f9f9;
}

/* .singlebtn {
    font-size: 18px;
}
.mutiplebtn {
    font-size: 18px;
} */



.filebox {
  position: relative;
}
.el-card__body {
  position: relative;
}
.el-upload--text {
  position: absolute;
  left: 22%;
  top: 14px;
}
.el-upload--text .el-button {
  background: rgb(170, 34, 144);
  border: none
}

.el-upload-list {
  width: 20%;
}
.filebtns {
  position: relative
}
.multifile {
  width: 100%;
}
.mutibtn {
  position: absolute;
  left: 12%;
  top: 0;
}
.uploadfile{
  padding: 4px 10px;
  height: 20px;
  line-height: 20px;
  position: relative;
  border: 1px solid #999;
  text-decoration: none;
  color: #666;
}
.change{
  position: absolute;
  overflow: hidden;
  right: 0;
  top: 0;
  opacity: 0;
}
.singleuploaded {

}
.mutiuploaded {
  left: 31.5%;
}
.outputloaded {
  left: 43%;
}
.inputfile-text {
  height: 33px;
  width: 233px;
  display: inline-block;
  border: 1px solid #ccc;
  line-height: 36px;
  border-radius: 7px;
  cursor: pointer;
}
.inputfile-text span {
  padding-left: 11px;
  color: #aba7a7;
}

.days li .current-month .full {
  background: #ff8a8a;
  text-align: center;
  line-height: 100px;
  color: #fff;

}

.days li .current-month .full .full-text {
  font-size: 26px;
}

.clearfix {clear: both}
.month .currentdate { float: left}
.month .choose {
  float: right;
  line-height: 57px;
  padding-right: 30px;
  color: #676767
}
.month .choose span {
  background: #fff;
  padding: 5px 10px;
  cursor: pointer;
}

/* upload */
.file {
  display: flex;
  width: 60px;
  height: 33px;
  overflow: hidden;
  border: 1px solid #ccc;
  width: 20%;
  margin-top: -4px;
  border-radius: 7px;
  position: relative;
}
.file .defaulttext {
  position: absolute;
  top: 5px;
  left: 10px;
  color: #bdbdbd;
}
.file input {
  width: 100%;
  height: 40px;
  position: relative;
  top: -10px;
  font-size: 0;
  opacity: 0;
  cursor: pointer;
}
.el-card__body #span {
  font-size: 14px;
  padding-left: 20px;
  color: #555;
  margin-top: 10px;
  display: block;
  width: 20%;
}
</style>

